/**
 * Created by Administrator on 2017/5/24.
 */

$(function () {









    function initialize() {

        var TreeViewO={
            data:[
            ]
        };
        $.ajax({
            url: ctx + '/rentnotice/selectNoticeCategoryTopsByCategoryName',//顶级查询
            data: {
                categoryName: ''
            },
            async:false,
            type: 'GET',
            success: function (data) {
                console.log(data);
                for(var i=0;i<data.content.length;i++){
                    (function () {
                        var index=i;
                        var val={
                            content:'',
                            subjoin:data.content[index],
                            onChange:function () {

                            }
                        };
                        var contentTop=$('<div class="contentTop"></div>');
                        var contentTopText=$('<span class="contentTopText"></span>');
                        contentTopText.append(data.content[index].categoryName);
                        var RedactButton=$('<span class="RedactButton"></span>');
                        var AddRedactButton=$('<span class="AddRedactButton"></span>');
                        contentTop.append(contentTopText);
                        contentTop.append(AddRedactButton);
                        contentTop.append(RedactButton);
                        RedactButton.ActionButtons({
                            data: '编辑',AttachData:{id:data.content[i].id}, onChange: function (e) {
                                $.ajax({
                                    url:ctx+'/rentnotice/selectCategoryTop',
                                    data:{
                                        id:e.data('data').id
                                    },
                                    type:'GET',
                                    success:function (data) {
                                        var Data=data;
                                        var AddOption = {
                                            data: {
                                                Title: '编辑类型',
                                                Content: '<div class="AddCategoryContent">' +
                                                '<div class="rows"><div class="Name">类型名称：</div><div class="Text"><input class="CategoryName" type="text" placeholder="请输入类别名称" value="'+data.categoryName+'"></div></div>' +
                                                '<div class="rows"><div class="Name">排序：</div><div class="Text"><input class="CategorySerial" type="number" placeholder="请输入类别排序号" value="'+data.seq+'"></div></div>' +
                                                '</div>',
                                                OkChange: function (e,Remove) {
                                                    var name=e.find('.CategoryName').val();
                                                    var serial=e.find('.CategorySerial').val();
                                                    if(name && serial && !name==''&& !serial==''){
                                                        var datas= {
                                                            categoryName:name,
                                                            id: Data.id,
                                                            seq: serial
                                                        };
                                                        $.ajax({
                                                            url: ctx+'/rentnotice/updateCategoryTop',//添加类型
                                                            data: datas,
                                                            type: 'POST',
                                                            success: function (data) {
                                                                if(data.message=='修改成功'){
                                                                    new $().Overlays({data:{text:'主类别修改成功！',state:'Ok'}});
                                                                    Remove();
                                                                    initialize();
                                                                }else {
                                                                    new $().Overlays({data:{text:'主类别修改失败，请稍后重试！',state:'No'}});
                                                                    Remove()
                                                                }
                                                            }
                                                        })
                                                    }else {
                                                        new $().Overlays({data:{text:'请检查您的信息是否正确！',state:'No'}});
                                                    }

                                                }
                                            },
                                            OKHide:true
                                        };
                                        new $('body').ModalBox(AddOption)
                                    }
                                });

                            }
                        });
                        AddRedactButton.ActionButtons({
                            data: '+ 添加子类型', onChange: function (e) {
                                var pid=data.content[index].id;
                                var categoryName=data.content[index].categoryName;
                                var AddOption = {
                                    data: {
                                        Title: '添加子类型',
                                        Content: '<div class="AddCategoryContent">' +
                                        '<div class="rows"><div class="Name">主类型名称：</div><div class="Text">'+categoryName+'</div>' +
                                        '<div class="rows"><div class="Name">子类型名称：</div><div class="Text"><input class="CategoryName" type="text" placeholder="请输入类别名称"></div></div>' +
                                        '<div class="rows"><div class="Name">排序：</div><div class="Text"><input class="CategorySerial" type="number" placeholder="请输入类别排序号"></div></div>' +
                                        '</div>',
                                        OkChange: function (e,Remove) {
                                            var name=e.find('.CategoryName').val();
                                            var serial=e.find('.CategorySerial').val();
                                            if(name && serial && !name==''&& !serial==''){
                                                var datas= {
                                                    categoryName:name,
                                                    seq: serial,
                                                    pid: pid,
                                                    topCategoryName: categoryName,
                                                    topType:data.content[index].topType
                                                };
                                                $.ajax({
                                                    url: ctx+'/rentnotice/insertCategory',//添加子类型
                                                    data: datas,
                                                    type: 'POST',
                                                    success: function (data) {
                                                        if(data.message=='创建成功'){
                                                            new $().Overlays({data:{text:'类别添加成功！',state:'Ok'}});
                                                            Remove();
                                                            initialize();
                                                        }else {
                                                            new $().Overlays({data:{text:'类别添加失败，请稍后重试！',state:'No'}});
                                                            initialize();
                                                        }
                                                    }
                                                })
                                            }else {
                                                new $().Overlays({data:{text:'请检查您的信息是否正确！',state:'No'}});
                                            }

                                        }
                                    },
                                    OKHide:true
                                };
                                new $('body').ModalBox(AddOption)
                            }
                        });
                        val.content=contentTop;

                        var TopcategoryName=data.content[index].categoryName;
                        $.ajax({
                            url: ctx + '/rentnotice/selectNoticeCategorysByCategoryName',//子类查询
                            type: 'GET',
                            data: {
                                pid: data.content[index].id,
                                categoryName:''
                            },
                            async:false,
                            success: function (data) {
                                if(data.content.length>0){
                                    val['child']=[];
                                    for(var i=0;i<data.content.length;i++){
                                        (function () {
                                            var indexChild=i;
                                            var child={
                                                content:data.content[indexChild].categoryName,
                                                subjoin:data.content[indexChild],
                                                onChange:function () {

                                                }
                                            };


                                            var contentchilds=$('<div class="contentchild"></div>');
                                            var contentTopTexts=$('<span class="contentTopText"></span>');

                                            var redactSon=$('<span class="redactSon"></span>');
                                            var deleteSon=$('<span class="deleteSon"></span>');
                                            redactSon.ActionButtons({
                                                data: '编辑', onChange: function (e) {
                                                    $.ajax({
                                                        url:ctx+'/rentnotice/selectCategory',
                                                        data:{
                                                            id:data.content[indexChild].id
                                                        },
                                                        type:"GET",
                                                        success:function (data) {
                                                            var pid=data.pid;
                                                            var id=data.id;
                                                            var AddOption = {
                                                                data: {
                                                                    Title: '修改子类型',
                                                                    Content: '<div class="AddCategoryContent">' +
                                                                    '<div class="rows"><div class="Name">主类型名称：</div><div class="Text">'+TopcategoryName+'</div>' +
                                                                    '<div class="rows"><div class="Name">子类型名称：</div><div class="Text"><input class="CategoryName" type="text" placeholder="请输入类别名称" value="'+data.categoryName+'"></div></div>' +
                                                                    '<div class="rows"><div class="Name">排序：</div><div class="Text"><input class="CategorySerial" type="number" placeholder="请输入类别排序号" value="'+data.seq+'"></div></div>' +
                                                                    '</div>',
                                                                    OkChange: function (e,Remove) {
                                                                        var name=e.find('.CategoryName').val();
                                                                        var serial=e.find('.CategorySerial').val();
                                                                        if(name && serial && !name==''&& !serial==''){
                                                                            var data= {
                                                                                id:id,
                                                                                categoryName:name,
                                                                                seq: serial,
                                                                                pid: pid,
                                                                                topCategoryName: TopcategoryName
                                                                            };
                                                                            $.ajax({
                                                                                url: ctx+'/rentnotice/updateCategory',//修改子类型
                                                                                data: data,
                                                                                type: 'POST',
                                                                                success: function (data) {
                                                                                    if(data.message=='修改成功'){
                                                                                        new $().Overlays({data:{text:'类别修改成功！',state:'Ok'}});
                                                                                        Remove();
                                                                                        initialize();

                                                                                    }else {
                                                                                        new $().Overlays({data:{text:'类别修改失败，请稍后重试！',state:'No'}});
                                                                                        Remove();
                                                                                        initialize();

                                                                                    }
                                                                                }
                                                                            })
                                                                        }else {
                                                                            new $().Overlays({data:{text:'请检查您的信息是否正确！',state:'No'}});
                                                                        }
                                                                    }
                                                                },
                                                                OKHide:true
                                                            };
                                                            new $('body').ModalBox(AddOption)
                                                        }
                                                    });

                                                }
                                            });
                                            var startBtion=true;
                                            deleteSon.ActionButtons({
                                                data: '删除', onChange: function (e) {
                                                    if(startBtion){
                                                        startBtion=false;
                                                        var deleteCategory={
                                                            data:{
                                                                Title:'删除提示',
                                                                Content:'是否删除这条子类型？',
                                                                OkChange:function () {
                                                                    $.ajax({
                                                                        url:ctx+'/rentnotice/deleteCategory',
                                                                        data:{
                                                                            id:data.content[indexChild].id
                                                                        },
                                                                        type:'POST',
                                                                        success:function (data) {
                                                                            if(data.message=='删除成功'){
                                                                                new $().Overlays({data:{text:'删除成功！',state:'Ok'}});
                                                                                initialize();

                                                                            }else {
                                                                                new $().Overlays({data:{text:'删除失败，请稍后重试！',state:'No'}});
                                                                                initialize();

                                                                            }

                                                                        }
                                                                    })
                                                                },
                                                                CancelChange:function () {
                                                                    startBtion=true;
                                                                }
                                                            }
                                                        };
                                                        new $('body').ModalBox(deleteCategory)
                                                    }

                                                }
                                            });
                                            contentTopTexts.append(data.content[indexChild].categoryName);
                                            contentchilds.append(contentTopTexts);
                                            contentchilds.append(deleteSon);
                                            contentchilds.append(redactSon);


                                            child.content=contentchilds;

                                            val.child.push(child)
                                        })()
                                    }
                                }

                            }
                        });

                        TreeViewO.data.push(val)
                    })();

                }
            }
        });
        $('.Category .category').html('');
        $('.Category .category').TreeView(TreeViewO);
    }
    initialize();


});